package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import org.springframework.web.bind.annotation.*;
import com.alibaba.dubbo.config.annotation.Reference;

@RestController
@RequestMapping(path = "/role")
@CrossOrigin
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页查询角色数据
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/page")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = roleService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //添加角色
    @PostMapping
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //根据用户ID查询，返回到页面
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        try {
            //查询角色基本信息
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //修改角色数据
    @PutMapping
    public Result update(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.update(role,permissionIds,menuIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

   //根据角色id删除角色
    //http://127.0.0.1:9000/role/delete/6
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable ("id")Integer id) {
        try {
            //删除角色
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //根据角色关联的ID查询菜单
    @GetMapping("/{id}/menu")
    public Result menuIds(@PathVariable("id") Integer id){
        try {
            //查询关联菜单ID数组
            Integer[] menuIds=roleService.menuById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //通过菜单ID查询权限数组
    @GetMapping("/{id}/permission")
    public Result permissionByIds(@PathVariable("id") Integer id){
        try {
            //查询关联权限ID
            Integer[] arr=roleService.permissionById(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,arr);
        }catch (Exception e){
        e.printStackTrace();
        return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

}
